using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Macaw.Moss2007.XmlSitemapSolution2
{
    class XmlSitemapJobFeatureReceiver2 : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureActivated start", this.GetType().FullName));
            SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;

            // Make sure the job isn't already registered.
            foreach (SPJobDefinition oldjob in webapp.JobDefinitions)
            {
                if (oldjob.Name == XmlSitemapJobDefinition2.JobName)
                    oldjob.Delete();
            }


            #region Daily Schedule
            XmlSitemapJobDefinition2 job = new XmlSitemapJobDefinition2(webapp);
            SPDailySchedule dailyschedule = new SPDailySchedule();
            dailyschedule.BeginHour = 0;
            dailyschedule.BeginMinute = 0;

            dailyschedule.EndHour = 6;
            dailyschedule.EndMinute = 0;

            job.Schedule = dailyschedule;
            job.Update();
            #endregion

            /*
            // Make sure the job isn't already registered.
            foreach (SPJobDefinition oldjob in webapp.JobDefinitions)
            {
                if (oldjob.Name == XmlSitemapJobDefinition2.JobName + "_now")
                    oldjob.Delete();
            }


            #region OneTime Schedule
            job = new XmlSitemapJobDefinition2(webapp);
            job.Name += "_now";
            job.Schedule = new SPOneTimeSchedule(DateTime.Now);
            job.Update();
            #endregion
            */
            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureActivated end", this.GetType().FullName));
  
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureDeactivating start", this.GetType().FullName));
            SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;

            // Delete the job.
            foreach (SPJobDefinition oldjob in webapp.JobDefinitions)
            {
                if (oldjob.Name == XmlSitemapJobDefinition2.JobName)
                    oldjob.Delete();

                if (oldjob.Name == XmlSitemapJobDefinition2.JobName + "_now")
                    oldjob.Delete();
            }

            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureDeactivating end", this.GetType().FullName));
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
